﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Adams.Shared
{
    public static class DateHelper
    {
        public static double DiffMins(DateTime d1, DateTime d2)
        {
            TimeSpan ts = d2 - d1;
            return Math.Floor(ts.TotalMinutes);
        }

        public static int DiffDays(DateTime d1, DateTime d2)
        {
            TimeSpan ts = d2 - d1;
            return ts.Days;
        }

        public static string GetMonthNameByNumber(int month)
        {
            switch (month)
            {
                case 1:
                    return "January";
                case 2:
                    return "February";
                case 3:
                    return "March";
                case 4:
                    return "April";
                case 5:
                    return "May";
                case 6:
                    return "June";
                case 7:
                    return "July";
                case 8:
                    return "August";
                case 9:
                    return "September";
                case 10:
                    return "October";
                case 11:
                    return "November";
                case 12:
                    return "December";
                default:
                    return "Unknown";
            }
        }

        public static string GetMonthShortNameByNumber(int month)
        {
            switch (month)
            {
                case 1:
                    return "Jan";
                case 2:
                    return "Feb";
                case 3:
                    return "Mar";
                case 4:
                    return "Apr";
                case 5:
                    return "May";
                case 6:
                    return "Jun";
                case 7:
                    return "Jul";
                case 8:
                    return "Aug";
                case 9:
                    return "Sep";
                case 10:
                    return "Oct";
                case 11:
                    return "Nov";
                case 12:
                    return "Dec";
                default:
                    return "NA";
            }
        }

        public static DateTime RoundUpToNextMinute(DateTime d, int increment)
        {
            DateTime ret;
            if (d.Minute % increment == 0)
            {
                ret = d;
            }
            else
            {
                ret = d.AddMinutes(-(d.Minute % increment)).AddMinutes(increment).AddSeconds(-(d.Second)).AddMilliseconds(-(d.Millisecond));
            }
            return ret;
        }

        public static DateTime RoundUpToNextHour(DateTime d)
        {
            DateTime ret = d.AddHours(1).AddMinutes(-(d.Minute)).AddSeconds(-(d.Second)).AddMilliseconds(-(d.Millisecond));
            return ret;
        }

        public static DateTime CombineDateTime(string date, string time)
        {
            return DateTime.Parse(DateTime.Parse(date).ToShortDateString() + " " + time);
        }

        public static int GetDayOccurance(DateTime d)
        {
            int dayofweek = (int)d.DayOfWeek;
            int date = d.Day;
            int occurance = (date / 7) + 1;
            return occurance;
        }

        static DateTime ConvertFromUnixTimestamp(double timestamp)
        {
            DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return origin.AddSeconds(timestamp);
        }

        static double ConvertToUnixTimestamp(DateTime date)
        {
            DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            TimeSpan diff = date - origin;
            return Math.Floor(diff.TotalSeconds);
        }
    }
}
